package com.service;

import com.bean.LogintokenContent;
import com.bean.LogintokenSource;
import com.mapper.mysql.LoginTokenMapper;
import com.mapper.repository.mysql.LogintokenContentRepository;
import com.mapper.repository.mysql.LogintokenSourceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class LogintokenServiceImpl implements LogintokenService{

    @Autowired
    private LoginTokenMapper loginTokenMapper;

    @Autowired
    private LogintokenSourceRepository logintokenSourceRepository;

    @Autowired
    private LogintokenContentRepository logintokenContentRepository;

    @Override
    public List<LogintokenSource> listLogintokenSource(LogintokenSource logintokenSource) {
        return loginTokenMapper.listLogintokenSource(logintokenSource);
    }

    @Override
    public String addLogintokenSource(LogintokenSource logintokenSource) {
        String id = UUID.randomUUID().toString();
        logintokenSource.setId(id);
        logintokenSource.setCreatedate(LocalDate.now());
        logintokenSourceRepository.save(logintokenSource);//新增来源
        return "新增成功";
    }

    @Override
    public String updateLogintokenSource(LogintokenSource logintokenSource) {
        logintokenSource.setUpdatedate(LocalDate.now());
        logintokenSourceRepository.save(logintokenSource);
        return "更新成功";
    }

    @Override
    public String addLogintokenContent(LogintokenContent logintokenContent) {
        String id = UUID.randomUUID().toString();
        logintokenContent.setId(id);
        logintokenContent.setCreatetime(LocalDateTime.now());
        logintokenContentRepository.save(logintokenContent);
        return "新增成功";
    }

    @Override
    public String updateLogintokenContent(LogintokenContent logintokenContent) {
        logintokenContentRepository.save(logintokenContent);
        return "更新成功";
    }

    @Override
    @Transactional
    public String delLogintoken(LogintokenSource logintokenSource) {
        logintokenContentRepository.deleteBySid(logintokenSource.getId());
        logintokenSourceRepository.deleteById(logintokenSource.getId());
        return "删除成功";
    }

    @Override
    public LogintokenSource getLogintoken(LogintokenSource logintokenSource) {
        return loginTokenMapper.getLogintoken(logintokenSource.getId());
    }
}
